<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Repositories\Manage\WemchRepository;
use Exception;

class WemchController extends ManageBaseController
{

    private $wemchRepo = NULL;

    public function __construct(
        WemchRepository $wemchRepo
    )
    {
        parent::__construct();
        $this->wemchRepo = $wemchRepo;
    }

    public function index()
    {
        $filter = request()->all();
        $res = $this->wemchRepo->getList($filter, $this->pageNum, $this->pageSize);
        return retSuc($res);
    }

    public function update($id = null)
    {
        $data = request()->only(['id', 'type', 'title', 'shop_id', 'status', "mch_id", "secret", "cert_path", "key_path"]);

        try {
            $last_id = $this->wemchRepo->saveOrUpdate($data, $id ?? 0);

            if (!$last_id) {
                return retFail(empty($data['id']) ? "新增失败" : "更新失败");
            }

            $res = $this->wemchRepo->getOne($last_id);
            return retSuc($res);
        } catch (Exception $e) {
            return retFail($e->getMessage());
        }

    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");
        $res = $this->wemchRepo->getOne($id);
        return retSuc($res);
    }

    public function delete($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return retFail("参数错误");
        }
        return $this->wemchRepo->del($id);
    }

}
